<?php
namespace app\admin\model;

use think\Db;
use think\facade\Config;
use think\Model;

class SmsReview extends Model
{
    // public static function getListAll($request)
    // {
    //     $tableName = 'bus_review_order_' . $request['table_time'];

    //     //检测表是否存在
    //     // $tableName = 'bus_review_order_' . $request['st'];

    //     $tableResult = Db::query('SHOW TABLES LIKE ' . "'" . $tableName . "'");

    //     if ($tableResult && $request['cid'] != '') {
    //         //先查对应通道，没有直接过滤
    //         $platWhere[] = ['csc.onoff', 'eq', 1];
    //         $platWhere[] = ['csc.cid', 'eq', $request['cid']];
    //         if ($request['platId'] != '') {
    //             $platWhere[] = ['bp.plat_id', 'eq', $request['platId']];
    //         }
    //         $platInfo = Db::table('bus_channel_send_config')->alias('csc')
    //             ->field('bp.plat_id,bp.platfrom,csc.cid')
    //             ->join('bus_platfrom bp', 'bp.plat_id = csc.plat_id')
    //             ->where($platWhere)
    //             ->select();
    //         $plat = '';
    //         if ($platInfo) {
    //             foreach ($platInfo as $key => $value) {
    //                 $plat .= '<span class="layui-badge layui-bg-orange status-jy">' . $value['platfrom'] . '</span>&nbsp;';
    //             }
    //             $where[] = ['o.status', 'eq', 0];
    //             // if (!empty($request['cid'])) {
    //             $where[] = ['o.cid', 'eq', $request['cid']];
    //             // }
    //             if ($request['phone_type'] != '') {
    //                 $where[] = ['o.phone_type', 'eq', $request['phone_type']];
    //             }

    //             if ($request['create_time'] != '') {
    //                 $where[] = ['o.create_time', '> time', $request['create_time']];
    //             }

    //             if ($request['end_time'] != '') {
    //                 $where[] = ['o.create_time', '< time', $request['end_time']];
    //             }
    //             // if ($request['state'] != '') {
    //             //     $where[] = ['o.status', 'eq', $request['state']];
    //             // }

    //             if ($request['send_text'] != '') {
    //                 $where[] = ['o.send_text', 'like', '%' . $request['send_text'] . '%'];
    //             }
    //             if ($request['phone'] != '') {
    //                 $where[] = ['rp.phone', 'eq', $request['phone']];
    //             }

    //             if ($request['isp_host'] != '') {
    //                 $where[] = ['rp.isp', 'eq', $request['isp_host']];
    //             }
    //             // dump($where);

    //             $data = Db::table($tableName)->alias('o')
    //                 ->field('o.*,rp.phone,c.company_name')
    //                 ->join('bus_channel c', 'o.cid = c.channel_id')
    //                 ->join('bus_review_phone_' . $request['table_time'] . ' rp', 'rp.table_order_id = o.id', 'RIGHT')
    //                 ->where($where)
    //                 ->group('o.id')
    //                 ->paginate($request['limit'], false, $request)
    //                 ->toarray();
    //             // halt($data);
    //             // halt(Db::table($tableName)->getLastSql());

    //             foreach ($data['data'] as $k => $v) {
    //                 //查询渠道对应的通道
    //                 switch ($v['result']) {
    //                     case '0':
    //                         $data['data'][$k]['result'] = '<span class="layui-badge layui-bg-orange status-jy">等待处理</span>';
    //                         break;
    //                     case '1':
    //                         $data['data'][$k]['result'] = '<span class="layui-badge layui-bg-green status-qy">已处理</span>';
    //                         break;
    //                     default:
    //                         $data['data'][$k]['result'] = '未定义';
    //                         break;
    //                 }
    //                 switch ($v['phone_type']) {
    //                     case '0':
    //                         $data['data'][$k]['phone_type'] = '<span class="layui-badge layui-bg-blue status-jy">群发</span>';
    //                         break;
    //                     case '1':
    //                         $data['data'][$k]['phone_type'] = '<span class="layui-badge layui-bg-green status-qy">单发</span>';
    //                         break;
    //                     default:
    //                         $data['data'][$k]['phone_type'] = '未定义';
    //                         break;
    //                 }
    //                 switch ($v['status']) {
    //                     case '0':
    //                         $data['data'][$k]['status'] = '<span class="layui-badge layui-bg-orange status-jy">待审核</span>';
    //                         break;
    //                     case '1':
    //                         $data['data'][$k]['status'] = '<span class="layui-badge layui-bg-green status-qy">通过</span>';
    //                         break;
    //                     case '2':
    //                         $data['data'][$k]['status'] = '<span class="layui-badge status-qy">失败</span>';
    //                         break;
    //                     default:
    //                         $data['data'][$k]['status'] = '未定义';
    //                         break;
    //                 }
    //                 switch ($v['submit_type']) {
    //                     case '0':
    //                         $data['data'][$k]['submit_type'] = '<span class="layui-badge layui-bg-orange status-jy">http</span>';
    //                         break;
    //                     case '1':
    //                         $data['data'][$k]['submit_type'] = '<span class="layui-badge layui-bg-cyan status-qy">cmpp</span>';
    //                         break;
    //                     default:
    //                         $data['data'][$k]['submit_type'] = '未定义';
    //                         break;
    //                 }
    //                 // if ($v['phone_size'] == '') {
    //                 //     $data['data'][$k]['phone_size'] = '无号码';
    //                 // } else {
    //                 // $data['data'][$k]['phone_size'] = '<a href="#">' . $v['phone_size'] . '</a>';
    //                 $data['data'][$k]['phone_size'] = '<a href="javascript:;" onclick="open_window(\'查看号码\',\'/admin/sms_review/smsPhone.html\',\'?table_order_id=' . $v['id'] . '&st=' . $request['table_time'] . '\',\'600\',\'400\')" style="color: #3498db;">查看号码</a>';
    //                 // }
    //                 $data['data'][$k]['company_name'] = $data['data'][$k]['company_name'] . '(' . $data['data'][$k]['cid'] . ')';
    //                 $data['data'][$k]['plat']         = $plat;
    //             }

    //         } else {
    //             $data['total'] = 0;
    //             $data['data']  = [];
    //             // halt('111');
    //         }

    //     } else {
    //         // halt('222');
    //         $data['total'] = 0;
    //         $data['data']  = [];
    //     }

    //     return $data;

    // }

    /**
     * [reviewSms 审核短信]
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    public static function reviewSms($request)
    {
        //检测表是否存在
        $tableName = 'bus_review_order_' . $request['st'];

        $tableResult = Db::query('SHOW TABLES LIKE ' . "'" . $tableName . "'");

        if ($tableResult) {
            $where[] = ['status', 'eq', 0];
            $where[] = ['id', 'In', json_decode($request['reviewId'], true)];
            $where[] = ['cid', 'eq', $request['cid']];
            $batchId = uniqid();
            $data    = [
                'desc'        => $request['desc'],
                'status'      => $request['status'],
                'batch_id'    => $batchId,
                'review_time' => date('Y-m-d H:i:s'),
                'username'    => session('username'),
            ];
            trace('表' . $tableName . '--审核短信--id:' . $request['reviewId']);
            Db::startTrans();
            try {
                $updateResult = Db::table($tableName)->where($where)->update($data);
                if ($updateResult != false) {
                    // 提交事务
                    Db::commit();
                    // $url         = 'http://183.240.150.13:3389/SMS/review/';
                    $url         = Config::get('SMS_REVIEW');
                    $requestData = [
                        'tableDate' => $request['st'],
                        'cid'       => $request['cid'],
                        'batchId'   => $batchId,
                        'type'      => $request['phone_type'],
                    ];
                    trace('短信审核请求java--' . json_encode($requestData));
                    $javaResult = curl_post($url, $requestData);
                    trace('请求返回---' . json_encode($javaResult));
                    
                    $result = ['code' => 200, 'msg' => '处理成功'];
                } else {
                    Db::rollback();
                    $result = ['code' => 422, 'msg' => '不存在需要审核的短信'];
                }
                //请求java
            } catch (\Exception $e) {
                trace('短信审核报错---' . $e);
                // 回滚事务
                Db::rollback();
                $result = ['code' => 422, 'msg' => '处理失败'];
            }

        } else {
            $result = ['code' => 422, 'msg' => '不存在该数据表'];
        }

        return $result;

    }

    /**
     * [batchReviewSms 批量审核短信]
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    public static function batchReviewSms($request)
    {
        //先查对应通道，没有直接过滤
        // $platWhere[] = ['csc.onoff', 'eq', 1];
        $platWhere[] = ['csc.cid', 'eq', $request['cid']];
        if ($request['platId'] != '') {
            $platWhere[] = ['bp.plat_id', 'eq', $request['platId']];
        }
        $platInfo = Db::table('bus_channel_send_config')->alias('csc')
            ->field('bp.plat_id,bp.platfrom,csc.cid')
            ->join('bus_platfrom bp', 'bp.plat_id = csc.plat_id')
            ->where($platWhere)
            ->select();
        //审核表
        $tableName = 'bus_review_order_' . $request['st'];

        $tableResult = Db::query('SHOW TABLES LIKE ' . "'" . $tableName . "'");
        // halt($tableResult);
        if ($platInfo && $request['cid'] != '' && is_numeric($request['number']) && $tableResult) {

            $where[] = ['o.status', 'eq', 0];
            $where[] = ['o.cid', 'eq', $request['cid']];
            // if ($request['phone_type'] != '') {
                $where[] = ['o.phone_type', 'eq', $request['phone_type']];
            // }

            if ($request['create_time'] != '') {
                $where[] = ['o.create_time', '> time', $request['create_time']];
            }

            if ($request['end_time'] != '') {
                $where[] = ['o.create_time', '< time', $request['end_time']];
            }

            // if ($request['channel_order_id'] != '') {
            //     $where[] = ['o.channel_order_id', 'eq', $request['channel_order_id']];
            // }
            if ($request['send_text'] != '') {
                $where[] = ['o.send_text', 'like', '%' . $request['send_text'] . '%'];
            }
            if ($request['phone'] != '') {
                $where[] = ['rp.phone', 'eq', $request['phone']];
            }

            if ($request['isp'] != '') {
                $where[] = ['rp.isp', 'eq', $request['isp']];
            }
            // halt($where);
            $orderIdArr = Db::table($tableName)->alias('o')
                ->field('o.*,rp.phone,c.company_name')

                ->join('bus_channel c', 'o.cid = c.channel_id')
                ->join('bus_review_phone_' . $request['st'] . ' rp', 'rp.table_order_id = o.id')
                ->where($where)
                ->group('o.id')
                ->limit($request['number'])
                ->column('o.id');
                // halt($orderIdArr);
            // return ['code' => '422', 'ccc' => Db::table($tableName)->getLastSql()];
            // die;
            trace('批量更新表' . $tableName . '----id:' . json_encode($orderIdArr));
            if ($orderIdArr) {
                $batchId = uniqid();
                $data    = [
                    'desc'        => $request['desc'],
                    'status'      => $request['status'],
                    'batch_id'    => $batchId,
                    'review_time' => date('Y-m-d H:i:s'),
                    'username'    => session('username'),
                ];
                Db::startTrans();
                try {
                    $updateResult = Db::table($tableName)->where('id', 'IN', $orderIdArr)->where('status', 'eq', 0)->where('cid', 'eq', $request['cid'])->update($data);
                    if ($updateResult != false) {
                        // $url         = 'http://183.240.150.13:3389/SMS/review/';
                        // 提交事务
                        Db::commit();
                        $url         = Config::get('SMS_REVIEW');
                        $requestData = [
                            'tableDate' => $request['st'],
                            'cid'       => $request['cid'],
                            'batchId'   => $batchId,
                            'type'      => $request['phone_type'],
                        ];
                        trace('批量审核请求java--' . json_encode($requestData));
                        $javaResult = curl_post($url, $requestData);
                        trace('批量请求返回---' . json_encode($javaResult));
                        
                        $result = ['code' => 200, 'msg' => '处理成功'];
                    } else {
                        Db::rollback();
                        $result = ['code' => 422, 'msg' => '不存在需要审核的短信'];
                    }
                    //请求java
                } catch (\Exception $e) {
                    trace('批量短信审核报错---' . $e);
                    // 回滚事务
                    Db::rollback();
                    $result = ['code' => 422, 'msg' => '处理失败'];
                }

                // halt($a);
            }
            // halt($orderIdArr);
        } else {
            $result = ['code' => 422, 'msg' => '无需要审核的短信'];
        }

        return $result;

    }

    /**
     * [reviewChannels 短信审核列表]
     * @return [type] [description]
     */
    public static function reviewChannels($request)
    {
        //检测表是否存在
        $tableName = 'bus_review_order_' . $request['st'];

        $tableResult = Db::query('SHOW TABLES LIKE ' . "'" . $tableName . "'");
        if ($tableResult) {
            
            $where[] = ['o.status', 'eq', 0];

            if (empty(session('relate_id'))) {
               return [];
            }
            $data = Db::table($tableName)->alias('o')
                ->field('o.status,o.cid,sum(o.phone_size) as phone_size,c.company_name,count(o.cid) as order_all')
                ->join('bus_channel c', 'o.cid = c.channel_id')
                ->where('o.cid',session('relate_id'))
                ->where($where)
                ->group('o.cid')
                ->paginate($request['limit'], false, $request)
                ->toarray();

            foreach ($data['data'] as $k => $v) {
                $data['data'][$k]['company_name'] = $data['data'][$k]['company_name'] . '(' . $data['data'][$k]['cid'] . ')';
                switch ($v['status']) {
                    case '0':
                        $data['data'][$k]['status'] = '<span class="layui-badge layui-bg-orange status-jy">待审核</span>';
                        break;
                    case '1':
                        $data['data'][$k]['status'] = '<span class="layui-badge layui-bg-green status-qy">通过</span>';
                        break;
                    case '2':
                        $data['data'][$k]['status'] = '<span class="layui-badge status-qy">失败</span>';
                        break;
                    default:
                        $data['data'][$k]['status'] = '未定义';
                        break;
                }
            }
        } else {
            $data['total'] = 0;
            $data['data']  = [];
        }
        return $data;
    }

    /**
     * [reviewOne 单条短信审核]
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    public static function reviewOne($request)
    {
        //检测表是否存在
        $tableName = 'bus_review_order_' . $request['table_time'];

        $tableResult = Db::query('SHOW TABLES LIKE ' . "'" . $tableName . "'");
        if ($tableResult) {
            $where[] = ['status', 'eq', 0];
            $where[] = ['id', 'eq', $request['reviewId']];
            $smsInfo = Db::table($tableName)->where($where)->find();

            if ($smsInfo) {
                $batchId = uniqid();
                $data    = [
                    'desc'        => $request['desc'],
                    'status'      => $request['status'],
                    'batch_id'    => $batchId,
                    'review_time' => date('Y-m-d H:i:s'),
                    'username'    => session('username'),
                ];
                Db::startTrans();
                try {
                    $updateResult = Db::table($tableName)->where($where)->update($data);
                    if ($updateResult != false) {
                        // $url         = 'http://183.240.150.13:3389/SMS/review/';
                        // 提交事务
                        Db::commit();
                        $url         = Config::get('SMS_REVIEW');
                        $requestData = [
                            'tableDate' => $request['table_time'],
                            'cid'       => $smsInfo['cid'],
                            'batchId'   => $batchId,
                            'type'      => $smsInfo['phone_type'],
                        ];
                        trace('单个审核请求java--' . json_encode($requestData));
                        $javaResult = curl_post($url, $requestData);
                        trace('单个审核请求返回---' . json_encode($javaResult));
                        
                        $result = ['code' => 200, 'msg' => '处理成功'];
                    } else {
                        Db::rollback();
                        $result = ['code' => 422, 'msg' => '不存在需要审核的短信'];
                    }
                    //请求java
                } catch (\Exception $e) {
                    trace('单个审核报错---' . $e);
                    // 回滚事务
                    Db::rollback();
                    $result = ['code' => 422, 'msg' => '处理失败'];
                }
            } else {
                trace('单个审核请求数据---' . json_encode($request));
                $result = ['code' => 422, 'msg' => '无该订单信息'];
            }
        } else {
            trace('单个审核无表信息---' . json_encode($request));
            $result = ['code' => 422, 'msg' => '无表：' . $request['table_time'] . '信息'];
        }
        return $result;
    }

    /**
     * [getAllSms 获取所有短信]
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    public static function getAllSms($request)
    {
        // halt('111');
        $tableName = 'bus_review_order_' . $request['table_time'];

        //检测表是否存在
        $tableResult = Db::query('SHOW TABLES LIKE ' . "'" . $tableName . "'");
        // halt('22233');
        if ($tableResult) {
            // halt('111');
            //先查对应通道，没有直接过滤
            // $platWhere[] = ['csc.onoff', 'eq', 1];
            // $platWhere[] = ['csc.cid', 'eq', $request['cid']];
            // if ($request['platId'] != '') {
            //     $platWhere[] = ['bp.plat_id', 'eq', $request['platId']];
            // }
            // $platInfo = Db::table('bus_channel_send_config')->alias('csc')
            //     ->field('bp.plat_id,bp.platfrom,csc.cid')
            //     ->join('bus_platfrom bp', 'bp.plat_id = csc.plat_id')
            //     ->where($platWhere)
            //     ->select();
            // $plat = '';
            // foreach ($platInfo as $key => $value) {
            //     $plat .= '<span class="layui-badge layui-bg-orange status-jy">' . $value['platfrom'] . '</span>&nbsp;';
            // }
            // $where[] = ['o.status', 'eq', 0];
            if ($request['cid'] != '') {
                $where[] = ['o.cid', 'eq', $request['cid']];
            }
            if ($request['phone_type'] != '') {
                $where[] = ['o.phone_type', 'eq', $request['phone_type']];
            }

            if ($request['create_time'] != '') {
                $where[] = ['o.create_time', '> time', $request['create_time']];
            }

            if ($request['end_time'] != '') {
                $where[] = ['o.create_time', '< time', $request['end_time']];
            }
            if ($request['state'] != '') {
                $where[] = ['o.status', 'eq', $request['state']];
            }

            if ($request['send_text'] != '') {
                $where[] = ['o.send_text', 'like', '%' . $request['send_text'] . '%'];
            }
            if ($request['phone'] != '') {
                $where[] = ['rp.phone', 'eq', $request['phone']];
            }

            if ($request['isp_host'] != '') {
                $where[] = ['rp.isp', 'eq', $request['isp_host']];
            }
            // dump($where);
            $data = Db::table($tableName)->alias('o')
                ->field('o.*,rp.phone,c.company_name')
                ->join('bus_channel c', 'o.cid = c.channel_id')
                ->join('bus_review_phone_' . $request['table_time'] . ' rp', 'rp.table_order_id = o.id', 'RIGHT')
                ->where($where)
                ->group('o.id')
                ->paginate($request['limit'], false, $request)
                ->toarray();
            // halt(Db::table($tableName)->getLastSql());
            if ($data['data']) {

                // dump($data);
                //拿到所有渠道去重
                $channelIdArr = array_unique(array_column($data['data'], 'cid'));
                // dump($channelIdArr);
                // dump($data);
                // $platWhere[] = ['csc.onoff', 'eq', 1];
                $platWhere[] = ['csc.cid', 'IN', $channelIdArr];
                if ($request['platId'] != '') {
                    $platWhere[] = ['bp.plat_id', 'eq', $request['platId']];
                }
                $platInfoArr = Db::table('bus_channel_send_config')
                    ->alias('csc')
                    ->field('csc.cid,bp.platfrom,bp.plat_id')
                    ->join('bus_platfrom bp', 'bp.plat_id = csc.plat_id')
                    ->where($platWhere)
                    ->select();
                //拿到所有通道对应的渠道id
                $platChannelIdArr = array_unique(array_column($platInfoArr, 'cid'));
                // dump($platChannelIdArr);
                // halt(Db::table('bus_channel_send_config')->getLastSql());
                // halt($data);
                // halt(Db::table($tableName)->getLastSql());
                // foreach ($platInfoArr as $key => $value) {
                //     // if ($data['data'][$k]['cid'] == $value['cid']) {
                //     dump($value['p'])
                //     // $data['data'][$k]['platname'] .= $value['plarfrom'];
                //     // }
                // }
                // die;
                if (empty($platChannelIdArr)) {
                    $data['total'] = 0;
                    $data['data']  = [];
                } else {

                    foreach ($data['data'] as $k => $v) {
                        $data['data'][$k]['plat'] = '';
                        foreach ($platInfoArr as $key => $value) {
                            //判断渠道是否配置了该通道
                            if (in_array($data['data'][$k]['cid'],$platChannelIdArr)) {

                                if ($data['data'][$k]['cid'] == $value['cid']) {

                                    $data['data'][$k]['plat'] .= '<span class="layui-badge layui-bg-orange status-jy">' . $value['platfrom'] . '</span>&nbsp;';
                                }
                                
                            }else{
                                unset($data['data'][$k]);
                            }
                        }

                        if (isset($data['data'][$k])) {
                            //查询渠道对应的通道
                                switch ($v['result']) {
                                    case '0':
                                        $data['data'][$k]['result'] = '<span class="layui-badge layui-bg-orange status-jy">等待处理</span>';
                                        break;
                                    case '1':
                                        $data['data'][$k]['result'] = '<span class="layui-badge layui-bg-green status-qy">已处理</span>';
                                        break;
                                    default:
                                        $data['data'][$k]['result'] = '未定义';
                                        break;
                                }
                                switch ($v['phone_type']) {
                                    case '0':
                                        $data['data'][$k]['phone_type'] = '<span class="layui-badge layui-bg-blue status-jy">群发</span>';
                                        break;
                                    case '1':
                                        $data['data'][$k]['phone_type'] = '<span class="layui-badge layui-bg-green status-qy">单发</span>';
                                        break;
                                    default:
                                        $data['data'][$k]['phone_type'] = '未定义';
                                        break;
                                }
                                switch ($v['status']) {
                                    case '0':
                                        $data['data'][$k]['status'] = '<span class="layui-badge layui-bg-orange status-jy">待审核</span>';
                                        break;
                                    case '1':
                                        $data['data'][$k]['status'] = '<span class="layui-badge layui-bg-green status-qy">通过</span>';
                                        break;
                                    case '2':
                                        $data['data'][$k]['status'] = '<span class="layui-badge status-qy">失败</span>';
                                        break;
                                    default:
                                        $data['data'][$k]['status'] = '未定义';
                                        break;
                                }
                                switch ($v['submit_type']) {
                                    case '0':
                                        $data['data'][$k]['submit_type'] = '<span class="layui-badge layui-bg-orange status-jy">http</span>';
                                        break;
                                    case '1':
                                        $data['data'][$k]['submit_type'] = '<span class="layui-badge layui-bg-cyan status-qy">cmpp</span>';
                                        break;
                                    default:
                                        $data['data'][$k]['submit_type'] = '未定义';
                                        break;
                                }
                                $data['data'][$k]['phone_size'] = '<a href="javascript:;" onclick="open_window(\'查看号码\',\'/admin/sms_review/smsPhone.html\',\'?table_order_id=' . $v['id'] . '&st=' . $request['table_time'] . '\',\'600\',\'400\')" style="color: #3498db;">查看号码</a>';
                                // }
                                $data['data'][$k]['company_name'] = $data['data'][$k]['company_name'] . '(' . $data['data'][$k]['cid'] . ')';
                                // $data['data'][$k]['plat']         = $plat;
                        }
                    }
                }
            } else {
                $data['total'] = 0;
                $data['data']  = [];
            }

        } else {
            // halt('222');
            $data['total'] = 0;
            $data['data']  = [];
        }

        return $data;

    }


    /**
     * [getListAll 获取所有未审核的]
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    public static function getListAll($request)
    {
        $tableName = 'bus_review_order_' . $request['table_time'];

        //检测表是否存在
        $tableResult = Db::query('SHOW TABLES LIKE ' . "'" . $tableName . "'");
        // halt('22233');
        if ($tableResult && $request['cid'] != '') {
            // halt('111');
            //先查对应通道，没有直接过滤
            // $platWhere[] = ['csc.onoff', 'eq', 1];
            // $platWhere[] = ['csc.cid', 'eq', $request['cid']];
            // if ($request['platId'] != '') {
            //     $platWhere[] = ['bp.plat_id', 'eq', $request['platId']];
            // }
            // $platInfo = Db::table('bus_channel_send_config')->alias('csc')
            //     ->field('bp.plat_id,bp.platfrom,csc.cid')
            //     ->join('bus_platfrom bp', 'bp.plat_id = csc.plat_id')
            //     ->where($platWhere)
            //     ->select();
            // $plat = '';
            // foreach ($platInfo as $key => $value) {
            //     $plat .= '<span class="layui-badge layui-bg-orange status-jy">' . $value['platfrom'] . '</span>&nbsp;';
            // }
            $where[] = ['o.status', 'eq', 0];
            if ($request['cid'] != '') {
                $where[] = ['o.cid', 'eq', $request['cid']];
            }
            if ($request['phone_type'] != '') {
                $where[] = ['o.phone_type', 'eq', $request['phone_type']];
            }

            if ($request['create_time'] != '') {
                $where[] = ['o.create_time', '> time', $request['create_time']];
            }

            if ($request['end_time'] != '') {
                $where[] = ['o.create_time', '< time', $request['end_time']];
            }
            // if ($request['state'] != '') {
            //     $where[] = ['o.status', 'eq', $request['state']];
            // }

            if ($request['send_text'] != '') {
                $where[] = ['o.send_text', 'like', '%' . $request['send_text'] . '%'];
            }
            if ($request['phone'] != '') {
                $where[] = ['rp.phone', 'eq', $request['phone']];
            }

            if ($request['isp_host'] != '') {
                $where[] = ['rp.isp', 'eq', $request['isp_host']];
            }

            // dump($where);
            $data = Db::table($tableName)->alias('o')
                ->field('o.*,rp.phone,c.company_name')
                ->join('bus_channel c', 'o.cid = c.channel_id')
                ->join('bus_review_phone_' . $request['table_time'] . ' rp', 'rp.table_order_id = o.id', 'RIGHT')
                ->where($where)
                ->group('o.id')
                ->paginate($request['limit'], false, $request)
                ->toarray();
            // halt(Db::table($tableName)->getLastSql());
            if ($data['data']) {

                // dump($data);
                //拿到所有渠道去重
                $channelIdArr = array_unique(array_column($data['data'], 'cid'));

                // $platWhere[] = ['csc.onoff', 'eq', 1];
                $platWhere[] = ['csc.cid', 'IN', $channelIdArr];
                if ($request['platId'] != '') {
                    $platWhere[] = ['bp.plat_id', 'eq', $request['platId']];
                }
                $platInfoArr = Db::table('bus_channel_send_config')
                    ->alias('csc')
                    ->field('csc.cid,bp.platfrom,bp.plat_id')
                    ->join('bus_platfrom bp', 'bp.plat_id = csc.plat_id')
                    ->where($platWhere)
                    ->select();
                //拿到所有通道对应的渠道id
                $platChannelIdArr = array_unique(array_column($platInfoArr, 'cid'));
                // dump($platInfoArr);
                // halt(Db::table('bus_channel_send_config')->getLastSql());
                // halt($data);
                // halt(Db::table($tableName)->getLastSql());
                // foreach ($platInfoArr as $key => $value) {
                //     // if ($data['data'][$k]['cid'] == $value['cid']) {
                //     dump($value['p'])
                //     // $data['data'][$k]['platname'] .= $value['plarfrom'];
                //     // }
                // }
                // die;
                if (empty($platChannelIdArr)) {
                    $data['total'] = 0;
                    $data['data']  = [];
                } else {

                    foreach ($data['data'] as $k => $v) {
                        $data['data'][$k]['plat'] = '';
                        foreach ($platInfoArr as $key => $value) {
                            //判断渠道是否配置了该通道
                            if (in_array($data['data'][$k]['cid'],$platChannelIdArr)) {

                                if ($data['data'][$k]['cid'] == $value['cid']) {

                                    $data['data'][$k]['plat'] .= '<span class="layui-badge layui-bg-orange status-jy">' . $value['platfrom'] . '</span>&nbsp;';
                                }
                                // $data['data'][$k]['plat']         = $plat;
                            }else{
                                unset($data['data'][$k]);
                            }
                        }
                        //通道条件查询下，判断通道不存在时，渠道审核短信是否被unset
                        if (isset($data['data'][$k])) {
                            //查询渠道对应的通道
                                switch ($v['result']) {
                                    case '0':
                                        $data['data'][$k]['result'] = '<span class="layui-badge layui-bg-orange status-jy">等待处理</span>';
                                        break;
                                    case '1':
                                        $data['data'][$k]['result'] = '<span class="layui-badge layui-bg-green status-qy">已处理</span>';
                                        break;
                                    default:
                                        $data['data'][$k]['result'] = '未定义';
                                        break;
                                }
                                switch ($v['phone_type']) {
                                    case '0':
                                        $data['data'][$k]['phone_type'] = '<span class="layui-badge layui-bg-blue status-jy">群发</span>';
                                        break;
                                    case '1':
                                        $data['data'][$k]['phone_type'] = '<span class="layui-badge layui-bg-green status-qy">单发</span>';
                                        break;
                                    default:
                                        $data['data'][$k]['phone_type'] = '未定义';
                                        break;
                                }
                                switch ($v['status']) {
                                    case '0':
                                        $data['data'][$k]['status'] = '<span class="layui-badge layui-bg-orange status-jy">待审核</span>';
                                        break;
                                    case '1':
                                        $data['data'][$k]['status'] = '<span class="layui-badge layui-bg-green status-qy">通过</span>';
                                        break;
                                    case '2':
                                        $data['data'][$k]['status'] = '<span class="layui-badge status-qy">失败</span>';
                                        break;
                                    default:
                                        $data['data'][$k]['status'] = '未定义';
                                        break;
                                }
                                switch ($v['submit_type']) {
                                    case '0':
                                        $data['data'][$k]['submit_type'] = '<span class="layui-badge layui-bg-orange status-jy">http</span>';
                                        break;
                                    case '1':
                                        $data['data'][$k]['submit_type'] = '<span class="layui-badge layui-bg-cyan status-qy">cmpp</span>';
                                        break;
                                    default:
                                        $data['data'][$k]['submit_type'] = '未定义';
                                        break;
                                }
                                $data['data'][$k]['phone_size'] = '<a href="javascript:;" onclick="open_window(\'查看号码\',\'/admin/sms_review/smsPhone.html\',\'?table_order_id=' . $v['id'] . '&st=' . $request['table_time'] . '\',\'600\',\'400\')" style="color: #3498db;">查看号码</a>';
                                // }
                                $data['data'][$k]['company_name'] = $data['data'][$k]['company_name'] . '(' . $data['data'][$k]['cid'] . ')';
                        }
                    }
                }
            } else {
                $data['total'] = 0;
                $data['data']  = [];
            }

        } else {
            // halt('222');
            $data['total'] = 0;
            $data['data']  = [];
        }

        return $data;

    }

}
